package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id") Long cid){
        // 调用service去查询规格组
        List<SpecGroupDTO> groupList = specService.querySpecGroupByCategoryId(cid);
        // 返回
        return ResponseEntity.ok(groupList);
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>>  querySpecParams(
            @RequestParam(value="gid", required = false) Long gid, // 规格组id
            @RequestParam(value="cid", required = false) Long cid, // 分类id
            @RequestParam(value="searching", required = false) Boolean searching  // 是否搜索
    ){
        // 返回
        return ResponseEntity.ok(specService.querySpecParams(gid, cid, searching));
    }


    /**
     * 根据分类id查询规格组及组内参数
     * @param cid
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCId(@RequestParam("id") Long cid){
        List<SpecGroupDTO> groupDTOList = specService.querySpecGroupByCId(cid);
        return ResponseEntity.ok(groupDTOList);
    }
}
